package com.tianqingwl.gsystem.resources.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.common.vo.FileUploadResultVo;
import com.tianqingwl.gsystem.resources.mapper.SysFileResourceMapper;
import com.tianqingwl.gsystem.resources.pojo.SysFileResource;
import com.tianqingwl.gsystem.resources.service.SysFileResourceService;
import com.tianqingwl.gsystem.sysuser.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysFileResourceServiceImpl implements SysFileResourceService {

    @Autowired
    private SysFileResourceMapper sysFileResourceMapper;

    @Override
    public FileUploadResultVo saveFile(MultipartFile[] files) throws Exception {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SysUser sysUser = (SysUser) authentication.getPrincipal();
        FileUploadResultVo fileUploadResultVo = new FileUploadResultVo();
        //TODO 文件上传存储方式可自定义
        //使用本地存储
        if (files != null && files.length > 0) {
            List<SysFileResource> fileResourceList = new ArrayList<>();
            String filePath = FileUtil.getAbsolutePath("static/upload");
            for (MultipartFile file : files) {
                //取基本信息、文件存储
                String fileName = file.getOriginalFilename();
                String ext = fileName.substring(fileName.lastIndexOf("."));
                String newName = IdUtil.simpleUUID();
                String newFileName = newName + ext;
                File uploadPathFile = new File(filePath);
                if (!uploadPathFile.exists()) {
                    uploadPathFile.mkdirs();
                }
                File dest = new File(filePath + File.separator + newFileName);
                file.transferTo(dest);
                //数据持久化
                SysFileResource resource = new SysFileResource();
                resource.setOriginalFileName(fileName);
                resource.setResCreateTime(new Date());
                resource.setResExt(ext.replace(".", ""));
                resource.setResName(newFileName);
                resource.setResPath(newFileName);
                resource.setResType(1);
                resource.setUserId(sysUser.getUserId());
                resource.setUserShowName(sysUser.getShowName());
                sysFileResourceMapper.insert(resource);
                fileResourceList.add(resource);
                //返回结果
                fileUploadResultVo.setOriginal(resource.getOriginalFileName());
                fileUploadResultVo.setTitle(resource.getResName());
                fileUploadResultVo.setUrl(resource.getResPath());
                fileUploadResultVo.setState("SUCCESS");
                fileUploadResultVo.setSuccess(true);
            }
            fileUploadResultVo.setData(fileResourceList);
            return fileUploadResultVo;
        }
        return null;
    }

    @Override
    public FileUploadResultVo saveFile(byte[] fileByte) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SysUser sysUser = (SysUser) authentication.getPrincipal();

        //TODO 文件上传存储方式可自定义
        //使用本地存储
        String filePath = FileUtil.getAbsolutePath("static/upload");
        //取基本信息、文件存储
        String ext = ".png";
        String newName = IdUtil.simpleUUID();
        String newFileName = newName + ext;
        String originalFileName = newFileName;
        File uploadPathFile = new File(filePath);
        if (!uploadPathFile.exists()) {
            uploadPathFile.mkdirs();
        }
        File target = new File(filePath + File.separator + newFileName);
        FileUtil.writeBytes(fileByte, target);
        //数据持久化
        SysFileResource resource = new SysFileResource();
        resource.setOriginalFileName(originalFileName);
        resource.setResCreateTime(new Date());
        resource.setResExt(ext.replace(".", ""));
        resource.setResName(newFileName);
        resource.setResPath(newFileName);
        resource.setResType(1);
        resource.setUserId(sysUser.getUserId());
        resource.setUserShowName(sysUser.getShowName());
        sysFileResourceMapper.insert(resource);
        return FileUploadResultVo.success(newFileName, newFileName, originalFileName, resource);
    }

    @Override
    public List<SysFileResource> listFileResource(Page<SysFileResource> fileResourcePage) {
        return sysFileResourceMapper.listSysFileResource(fileResourcePage);
    }
}
